//
//  MBProgressHUD+WNExpand.m
//  iOSOCProjectDirectory
//
//  Created by BHmac on 2017/6/9.
//  Copyright © 2017年 XieXuan. All rights reserved.
//

#import "MBProgressHUD+WNExpand.h"

#define Time 1.5

@implementation MBProgressHUD (WNExpand)

+(MBProgressHUD *)createMBProgressHUD{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:[self getCurrentViewController].view animated:YES];
    return hud;
}

+ (UIViewController *)getCurrentViewController
{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        result = [(UITabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    return result;
}


+(void)showLoadMessage{
    [self createMBProgressHUD];
    
}
+(void)showLoadMessage:(NSString *)message{
    MBProgressHUD * hud = [self createMBProgressHUD];
    hud.labelText = message;
}
//函进度条
+(void)showLoadMessageWithBarProgress:(CGFloat)progress{
    [self showMessage:nil Progress:progress model:MBProgressHUDModeDeterminateHorizontalBar];
}
+(void)showLoadMessage:(NSString *)message barProgress:(CGFloat)progress{
    [self showMessage:message Progress:progress model:MBProgressHUDModeDeterminateHorizontalBar];
}
//圆形进度条
+(void)showLoadMessageWithAnnularProgress:(CGFloat)progress{
    [self showMessage:nil Progress:progress model:MBProgressHUDModeAnnularDeterminate];
}
+(void)showLoadMessage:(NSString *)message annularProgress:(CGFloat)progress{
    [self showMessage:message Progress:progress model:MBProgressHUDModeAnnularDeterminate];
}
+(void)showMessage:(NSString *)message Progress:(CGFloat)progress model:(MBProgressHUDMode)model{
    MBProgressHUD * hud = [self createMBProgressHUD];
    hud.mode = model;
    hud.labelText = message;
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD HUDForView:[self getCurrentViewController].view].progress = progress;
        });
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hide:YES];
        });
    });
    
}
//只显示文字
+(void)showText:(NSString *)text{
    [self showText:text model:MBProgressHUDModeText icon:nil];
}

+(void)showSuccess:(NSString *)success{
    [self showText:success model:MBProgressHUDModeCustomView icon:@"success.png"];
}
+(void)showError:(NSString *)error{
    [self showText:error model:MBProgressHUDModeCustomView icon:@"error.png"];
}

+(void)showText:(NSString *)text model:(MBProgressHUDMode)model icon:(NSString *)icon{
    MBProgressHUD * hud = [self createMBProgressHUD];
    hud.mode = model;
    hud.labelText = text;
    //    hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    [hud hide:YES afterDelay:Time];
}

+ (void)hideHUD{
    [self hideHUDForView:[self getCurrentViewController].view animated:YES];
    
}


@end
